package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.CompanyPhotoModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyPhotoRequest;
import one.stand.response.CompanyPhotoResponse;
import one.stand.service.CompanyPhotoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Transactional
public class CompanyPhotoServiceImpl extends PublicBaseService implements CompanyPhotoService {

    @Override
    public ResultModel<Map<String, Object>> selectCompanyPhotoPage(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getCId()) {
            return ResultModel.checkParamFail("cId为空");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        CompanyPhotoModel model = new CompanyPhotoModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        List<CompanyPhotoModel> list = companyPhotoMapper.select(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<CompanyPhotoResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                CompanyPhotoResponse response = new CompanyPhotoResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        PageInfo<CompanyPhotoResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<List<CompanyPhotoResponse>> selectCompanyPhoto(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getCId()) {
            return ResultModel.checkParamFail("cId为空");
        } else if (null == request.getType()) {
            return ResultModel.checkParamFail("type为空");
        }
        CompanyPhotoModel model = new CompanyPhotoModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        List<CompanyPhotoModel> list = companyPhotoMapper.select(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<CompanyPhotoResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                CompanyPhotoResponse response = new CompanyPhotoResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Boolean> insertCompanyPhoto(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getCId()) {
            return ResultModel.checkParamFail("cId为空");
        } else if (null == request.getType()) {
            return ResultModel.checkParamFail("type为空");
        }
        companyPhotoMapper.delCompanyPhoto(new CompanyPhotoModel() {{
            setCompanyId(request.getCId().intValue());
            setType(request.getType());
        }});
        if (request.getPhotoList() != null) {
            for (String photo : request.getPhotoList()) {
                CompanyPhotoModel model = new CompanyPhotoModel();
                model.setCompanyId(request.getCId().intValue());
                model.setIsDel(0);
                model.setCreateTime(currentDate());
                model.setSort(companyPhotoMapper.getMaxSort(new CompanyPhotoModel() {{
                    setCompanyId(request.getCId().intValue());
                    setType(request.getType());
                }}));
                model.setType(request.getType());
                model.setPhoto(photo);
                companyPhotoMapper.insertSelective(model);
            }
        }
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<List<Map<String, Object>>> selectTypeNum(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getCId()) {
            return ResultModel.checkParamFail("cId为空");
        }
        List<Map<String, Object>> list = companyPhotoMapper.selectTypeNum(request.getCId().intValue());
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        return ResultModel.success(list);
    }

    @Override
    public ResultModel<Boolean> moveCompanyPhoto(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getCId()) {
            return ResultModel.checkParamFail("cId为空");
        } else if (null == request.getType()) {
            return ResultModel.checkParamFail("type为空");
        } else if (null == request.getSortType()) {
            return ResultModel.checkParamFail("sortType为空");
        }
        CompanyPhotoModel model = new CompanyPhotoModel();
        model.setCompanyId(request.getCId().intValue());
        model.setSortType(request.getSortType());
        model.setPhotoId(request.getPhotoId());
        CompanyPhotoModel companyPhotoModel = companyPhotoMapper.selectThanSort(model);
        if (companyPhotoModel == null) {
            if (ONE.equals(request.getSortType())) {
                return ResultModel.fail("到顶了，无法上移！");
            } else if (TWO.equals(request.getSortType())) {
                return ResultModel.fail("到底了，无法下移！");
            }
        }
        // 修改排序
        model.setSort(companyPhotoModel.getSort());
        if (companyPhotoMapper.updateByPrimaryKeySelective(model) > 0) {
            model = new CompanyPhotoModel();
            model.setPhotoId(companyPhotoModel.getPhotoId());
            model.setSort(companyPhotoModel.getLastSort());
            companyPhotoMapper.updateByPrimaryKeySelective(model);
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel<Boolean> delCompanyPhoto(CompanyPhotoRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (null == request.getPhotoId()) {
            return ResultModel.checkParamFail("photoId为空");
        }
        CompanyPhotoModel model = new CompanyPhotoModel();
        model.setPhotoId(request.getPhotoId());
        model.setIsDel(1);
        companyPhotoMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }
}
